﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chessLogic
{
    public class Pawn : Figure
    {
        public Pawn(FigureColor color) : base(color, FigureType.Pawn) { }

        public override List<Position> GetPossiblyPositionNormalMove(Position position, Board board)
        {
            List<Position> retList = new List<Position>();
            int increment = (Color == FigureColor.White) ? 1 : -1;

            Position possiblyPosition = new Position(position.Letter, position.Number + increment);
            if (possiblyPosition.IsOnBoard() && board.IsPositionEmpty(possiblyPosition))
            {
                retList.Add(possiblyPosition);

                possiblyPosition = new Position(position.Letter, position.Number + 2 * increment);

                if (possiblyPosition.IsOnBoard() && board.IsPositionEmpty(possiblyPosition))
                    if (position.Number == 1 || position.Number == 6)
                        retList.Add(possiblyPosition);
            }
            return retList;
        }
        public override List<Position> GetPossiblyPositionTakingMove(Position position, Board board)
        {
            List<Position> retList = new List<Position>();
            int increment = (Color == FigureColor.White) ? 1 : -1;
            string letters = "XabcdefghX";
            int index = letters.IndexOf(position.Letter);

            Position pos = new Position(letters[index + 1], position.Number + increment);

            if (pos.IsOnBoard())
                if (!board.IsPositionEmpty(pos))
                    retList.Add(pos);

            pos = new Position(letters[index - 1], position.Number + increment);
            if (pos.IsOnBoard())
                if (!board.IsPositionEmpty(pos))
                    retList.Add(pos);

            return retList;
        }
        public static List<Position> GetPossiblyPositionPassantMove(Position position, Board board,FigureColor player)
        {
            List<Position> retList = new List<Position>();
            int increment = (player == FigureColor.White) ? 1 : -1;
            string letters = "XabcdefghX";
            int index = letters.IndexOf(position.Letter);

            Position pos = new Position(letters[index + 1], position.Number + increment);
            if (pos.IsOnBoard() && board.IsPositionEmpty(pos)
                                    && !board.IsPositionEmpty(new Position(pos.Letter, pos.Number - increment))
                                                         && board.getFigure(new Position(pos.Letter, pos.Number - increment)).Color != player)
                retList.Add(pos);

            pos = new Position(letters[index - 1], position.Number + increment);
            if (pos.IsOnBoard() && board.IsPositionEmpty(pos)
                                    && !board.IsPositionEmpty(new Position(pos.Letter, pos.Number - increment))
                                                         && board.getFigure(new Position(pos.Letter, pos.Number - increment)).Color != player)
                retList.Add(pos);

            return retList;
        }
    }
}
